package com.dzt.androidframework.network.mobapi.cook;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.TitleFrameActivity;
import com.dzt.androidframework.databinding.ActivityCookMenuBinding;
import com.dzt.androidframework.network.mobapi.cook.CookMenu.ResultBean.ListBean;
import com.dzt.androidframework.utils.JLogKit;

import java.util.ArrayList;
import java.util.List;

import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

public class CookMenuActivity extends TitleFrameActivity<ActivityCookMenuBinding>
		implements CookMenuView {

	private CookMenuAdapter adapter;
	private List<ListBean> list = new ArrayList<>();
	private CookMenuPresenter presenter;

	@Override
	protected void initData(Bundle savedInstanceState) {
		presenter = new CookMenuPresenter();
		presenter.onCreate();
		presenter.attachView(this);
	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_cook_menu;
	}

	@Override
	protected void initWidgets() {
		setTitle("菜谱列表");
		String cid = getIntent().getStringExtra("cid");
		presenter.queryCookMenu(cid, 1, 20);
		adapter = new CookMenuAdapter(context, list, R.layout.item_cook_menu);
		LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
		bindingView.recyclerview.setLayoutManager(layoutManager);
		bindingView.recyclerview.setItemAnimator(new SlideInLeftAnimator());
		bindingView.recyclerview.setAdapter(new ScaleInAnimationAdapter(adapter));
		adapter.setOnItemClickListener(new onItemClickListener());
	}

	public class onItemClickListener implements CookMenuAdapter.OnItemClickListener {
		@Override
		public void onItemClick(View view, int position) {
			JLogKit.getInstance().e("onItemClick pos = " + position);
			Intent intent = new Intent();
			String cid = list.get(position).getMenuId();
			intent.putExtra("cid", cid);
			startActivity(CookActivity.class, intent);
		}
	}

	@Override
	public void onSuccess(CookMenu menu) {
		list.clear();
		list.addAll(menu.getResult().getList());
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onError(String result) {

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		presenter.onDestroy();
	}
}
